package com.peas.platform.module.icims.budget.apply.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.apply.adapter.repository.BudgetOneCollectRepository;
import com.peas.platform.module.icims.budget.apply.adapter.repository.po.BudgetOneCollectPo;
import com.peas.platform.module.icims.budget.apply.domain.BudgetOneCollectDomain;
import com.peas.platform.module.icims.budget.apply.dto.BudgetOneCollectDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetOneCollectRelationDto;
import com.peas.platform.module.icims.budget.apply.service.BudgetOneCollectRelationService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class BudgetOneCollectDomainImpl extends BaseDomainImpl<BudgetOneCollectDto, BudgetOneCollectPo> implements BudgetOneCollectDomain {

    @Resource
    private BudgetOneCollectRepository budgetOneCollectRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private BudgetOneCollectRelationService budgetOneCollectRelationService;

    @Override
    public BaseRepository<BudgetOneCollectPo> getMapper() {
        return budgetOneCollectRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetOneCollectPo>> getRepositoryClass() {
        return BudgetOneCollectRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetOneCollectDto dto) {
        if (StrUtil.isBlank(dto.getBudgetOneCollectCode())) {
            String code = sequenceService.genSequence("base", "BudgetOneCollectDto", "BOC", 4, 1);
            dto.setBudgetOneCollectCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("budgetOneCollectCode", dto.getBudgetOneCollectCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetOneCollectDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("budgetOneCollectCode", dto.getBudgetOneCollectCode());
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        List<BudgetOneCollectRelationDto> bocrds = budgetOneCollectRelationService.listByField("budgetOneCollectId", id);
        budgetOneCollectRelationService.removeBatchByList(bocrds);
        return super.removeById(id);
    }

}
